<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 * zy_plan_reply_mc Database of the driver class.
 *
 * @Author:    <>zaki
 * @Package:   Database Operate class of the system.
 * @Copyright: Copyright (c) 2005 - 2006 
 * @Version:   $Id: DB_zy_plan_reply_mc.class.php 2011-07-07 10:01:35 $
 * @Show Note: 
 */
/* @Package of the database class */
Class DB_zy_plan_reply_mc extends PdoMysql
{
    /* Mysql Table Name */
    public $tableName = "zy_plan_reply_mc";

    /**
     * Construction this Database Object.
     *
     * @Author: zaki.
     * @Show: Get the PdoMysql Object and Set the default Charset.
     * @Return: ..
     */
    public function __construct()
    {
    		parent::__construct();
    }

    /**
     * Execute the insert method.
     *
     * @Author: zaki.
     * @Show: Execute 'insert' method by the PdoMysql Object.
     * @Param: $values Array -- Key is the Table Coulnum and Value is that values.
     * @Return: False OR int.
     */
    public function insert_zy_plan_reply_mc($values)
    {
        return $this->insert($this->tableName, $values);
    }

    /**
     * Execute the update method.
     *
     * @Author: zaki.
     * @Show: Execute 'update' method by the PdoMysql Object.
     * @Param: $values Array -- Key is the Table Coulnum and Value is that values.
     * @Param: @where  Array -- Key is the Table Coulnum and Value is the where values.
     * @Return: False OR int.
     */
    public function update_zy_plan_reply_mc($values, $where)
    {
        return $this->update($this->tableName, $values, $where);
    }
    
    /**
     * Execute the delete method.
     *
     * @Author: zaki.
     * @Show: Execute 'delete' method by the PdoMysql Object.
     * @Param: @where  Array -- Key is the Table Coulnum and Value is the where values.
     * @Return: False OR int.
     */
    public function delete_zy_plan_reply_mc($where)
    {
        return $this->delete($this->tableName, $where);
    }

    /**
     * Execute the select method.
     *
     * @Author: zaki
     * @Show: Execute 'select' method by the PdoMysql Object, Return the item set.
     * @Param: $where String -- Search where.
     * @Param: $col String -- Search Order.
     * @Param: $numrows Int -- Search item num.
     * @Param: $offset Int -- Search item set offset.
     * @Param: $field String -- Search item name.
     * @Return: False OR Array.
     */
    public function getAll($where='1', $col='', $numrows=-1, $offset=-1, $field="*")
    {
        $sql = sprintf("SELECT %s FROM %s WHERE %s", $field, $this->tableName, $where);
        return $this->select($sql, $col, $numrows, $offset);
    }

    /**
     * Get one Item.
     *
     * @Author: zaki
     * @Show: Search the database.Return one item.
     * @Param: $index Array -- Key is the index Key.Value is the index value.
     * @Return: Array.
     */
    public function getOne($plan_reply_mc_id)
    {
        $sql = sprintf("SELECT * FROM %s WHERE plan_reply_mc_id = '%s'", $this->tableName, $plan_reply_mc_id);
        $result = $this->select($sql, 'plan_reply_mc_id', 1, 0);
        return isset($result[0]) ? $result[0] : array();
    }
    /**
     * 判断批复设备关联表是否存在虚拟设备ID
     *
     * @param unknown_type $mc_id
     * @return 0 不存在 1存在
     */
    function check_is_exist_by_mc_id($mc_id)
    {
    	$where = " v_mc_id = {$mc_id} ";
    	$temp = $this->getAll($where,'plan_reply_mc_id',1,0);
		return is_array($temp) && !empty($temp) ? 1 : 0;    		
    }
    /**
     * 根据批复ID取得关联表中设备数据
     *
     * @param unknown_type $plan_reply_id
     * @return unknown
     */
    function get_checked_v_mc($plan_reply_id)
    {
    	$return_arr = array();
    	$where = " plan_reply_id = {$plan_reply_id} ";
    	$temp = $this->getAll($where,'plan_reply_mc_id','','','v_mc_id,plan_reply_mc_number,plan_reply_mc_all_cost,mc_cn_name');
    	if (is_array($temp) && !empty($temp)) {
    		foreach ($temp as $value)
    		{
    			$return_arr[$value['v_mc_id']] = $value;
    		}
    	}
    	return $return_arr;
    }
    /**
     * 检测关于某个批复ID是否已经被执行(是否已经生成合同)
     */
    function check_is_reply_do($plan_reply_id)
    {
    	if (empty($plan_reply_id)) {
    		return false;
    	}
    	$sql = sprintf( " SELECT b.plan_reply_mc_id FROM zy_sell_reply as b left join zy_plan_reply_mc as a  on a.plan_reply_mc_id = b.plan_reply_mc_id WHERE a.plan_reply_id = %s",$plan_reply_id);
    	$re = $this->query($sql);
    	return $re->fetchAll(PDO::FETCH_ASSOC);
    }
    /**
     * 根据批复ID取得设备信息
     *
     * @param unknown_type $plan_reply_id
     * @return unknown
     */
    function get_reply_mc_info($plan_reply_id)
    {
    	global $_GET;
    	$sql = sprintf( " SELECT * FROM zy_plan_reply_mc as a left join zy_mc_type as b on a.v_mc_id = b.v_mc_id WHERE a.plan_reply_id = %s",$plan_reply_id);
    	$cn_name = isset($_GET['cn_name']) ? trim($_GET['cn_name']) : '';
    	if($cn_name!=''){
    		$sql = $sql." and a.mc_cn_name like '%{$cn_name}%'";
    	}
    	//echo $sql;
    	$re = $this->query($sql);
    	return $re->fetchAll(PDO::FETCH_ASSOC);
    }
    /**
     * 根据投资计划编号取得未被执行完成的设备信息
     *
     * @param unknown_type $investment_no
     * @return unknown
     */
    function get_mc_by_investment_no($investment_no)
    {
    	echo "2222";
    	$return_arr = array();
    	$where = " investment_no = '{$investment_no}' AND sell_state = 0 ";
    	$temp = $this->getAll($where,'plan_reply_mc_id');
    	if (is_array($temp) && !empty($temp)) {
    		foreach ($temp as $value) {
    			$return_arr[$value['plan_reply_mc_id']] = $value;
    		}
    	}
    	return $return_arr;
    }
    /**
     * 根据批复设备关联表中ID数组取得数据信息
     *
     * @param unknown_type $id_arr 批复设备关联表中ID数组
     */
    function get_mc_by_id($id_arr)
    {
    	$return_arr = array();
    	$id_str = implode(',',$id_arr);
    	$where = " plan_reply_mc_id IN ({$id_str}) ";
    	$temp = $this->getAll($where,'plan_reply_mc_id');
    	if (is_array($temp) && !empty($temp)) {
    		foreach ($temp as $value) {
    			$return_arr[$value['plan_reply_mc_id']] = $value;
    		}
    	}
    	return $return_arr;
    }
    /**
     * 根据批复设备关联ID数组取得虚拟设备信息
     *
     * @param unknown_type $plan_reply_mc_id_arr
     */
    function get_mc_info_by_id_arr($plan_reply_mc_id_arr)
    {
    	if (empty($plan_reply_mc_id_arr)) {
    		return false;
    	}
    	$plan_reply_mc_id_arr = is_array($plan_reply_mc_id_arr) ? $plan_reply_mc_id_arr : array($plan_reply_mc_id_arr);
    	$id_str = implode(',',$plan_reply_mc_id_arr);
    	$sql = sprintf( " SELECT * FROM zy_plan_reply_mc as a left join zy_mc_type as b on a.v_mc_id = b.v_mc_id WHERE a.plan_reply_mc_id IN (%s)",$id_str);
    	global $_GET;
    	$cn_name = isset($_GET['cn_name']) ? trim($_GET['cn_name']) : '';
    	if($cn_name!=''){
    		$sql = $sql." and a.mc_cn_name like '%{$cn_name}%'";
    	}
    	$re = $this->query($sql);
    	return $re->fetchAll(PDO::FETCH_ASSOC);
    }
    /**
     * 根据设备类型ID取得设备被批复的数量
     *
     * @param unknown_type $v_mc_id
     * @return unknown
     */
    function plan_reply_num_total($v_mc_id)
    {
    	$sql = " SELECT SUM(plan_reply_mc_number) as num FROM zy_plan_reply_mc WHERE v_mc_id = {$v_mc_id} ";
    	$re = $this->query($sql);($sql);
    	$temp = $re->fetchAll(PDO::FETCH_ASSOC);
    	return isset($temp[0]['num']) ? $temp[0]['num'] : 0;
    }
    /**
     * 根据批复ID数组取得批复关联表中设备信息
     *
     * @param unknown_type $plan_reply_id_arr
     * @return unknown
     */
    function get_mc_info_by_plan_id_arr($plan_reply_id_arr,$is_show=0)
    {
    	$str = implode(',',$plan_reply_id_arr);
    	$where = " plan_reply_id IN ({$str}) ";
    	if (empty($is_show)) {
    		$where .= "  AND sell_state = 0 ";
    	}else {
    		$where .= "  AND sell_state = 1 ";
    	}
    	$temp = $this->getAll($where);
    	//查询是否存在单位
    	$mc_type_db = Factory::getDB('zy_mc_type');
    	if(isset($temp[0])){
    		foreach ($temp as $k=>$v){
    			$temp_arr = $mc_type_db->getOne($v['v_mc_id']);
    			$temp[$k]['unit'] = $temp_arr['unit'];
    		}
    	}
    	return is_array($temp) ? $temp : array();
    }
    /**
     * 根据批复设备关联表ID取得批复数据
     *
     * @param unknown_type $plan_reply_mc_id
     */
    function get_plan_info($plan_reply_mc_id)
    {
    	$plan_reply_mc_id = intval($plan_reply_mc_id);
    	$sql = sprintf( " SELECT * FROM zy_plan_reply_mc as a left join zy_plan_reply as b on a.plan_reply_id = b.plan_reply_id WHERE a.plan_reply_mc_id = (%d)",$plan_reply_mc_id);
    	$re = $this->query($sql);
    	$temp = $re->fetchAll(PDO::FETCH_ASSOC);
    	return isset($temp[0]) && is_array($temp[0]) ? $temp[0] : array();
    }
}

/* Public function is the Class method, Design by Hooker framework designer */
/* CreateTime: 2011-07-07 10:01:35 */

?>